/**
 * 
 */
package org.cuatrovientos.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.apache.log4j.Logger;
import org.cuatrovientos.erp.business.SaleBO;
import org.cuatrovientos.erp.model.Sale;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * @author Daniel Lacasta
 */
@Controller
public class SaleController {

	// Get log4j logger
	private static final Logger logger = Logger.getLogger(SaleController.class);

	@Autowired
    private SaleBO saleBO;
    
        /**
         * default constructor
         */
        public SaleController () {
               logger.info("Sale BEAN instantiated");
                
        }
        
        
        
        /**
         * handles default /sale or /sale/list request
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/sale","/sale/list"})
         */
        @RequestMapping({"/sale","/sale/list"})
        public ModelAndView saleDefault() {
        	logger.info("default");
 
                Map<String, Object> model = new HashMap<String, Object>();

                List<Sale> saleList = saleBO.selectAll();
                model.put("sales", saleList );
                return new ModelAndView("SaleList", model);     
        }



        /**
         * handles delete sale, and redirect to sale default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/sale","/sale/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/sale/delete"})
        public String deleteSale(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("delete " + id);

            saleBO.deleteById(id);
            return "redirect:/sale";

        }
        
        /**
         * handles new sale request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it: in this case "sale"
         * @return the name of the view to show
         * @RequestMapping({"/sale/new"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/sale/new"})
        public ModelAndView newSale() {
        		logger.info("new sale ");
                Sale sale = new Sale();
               
                return new ModelAndView("NewSale", "sale", sale);
        }
        
        /**
         * handles new sale request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/sale/new"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/sale/save"})
        public String newSale(@Valid Sale sale, BindingResult bindingResult) {
        		logger.info("new sale " + sale.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("sale", sale);
        			return "NewSale";
        		}
        		
        		// If data is ok, insert and go on
                saleBO.insert(sale);
                return "redirect:/sale";
        }
        
        /**
         * handles delete sale, and redirect to sale default
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/sale","/sale/list"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/sale/detail"})
        public ModelAndView saleDetail(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("Detail " + id);
            Map<String,Object> model = new HashMap<String,Object>();
            Object sale = saleBO.selectById(id);
            model.put("sale", sale);
            
            return new ModelAndView("SaleDetail",model);

        }
        
        /**
         * handles sale update, first part: register loaded
         * @param model
         * @return the name of the view to show
         *  @RequestMapping({"/sale/update"})
         */
        @RequestMapping(method=RequestMethod.GET,value={"/sale/update"})
        public ModelAndView update(@RequestParam(value="id", defaultValue="", required=true) int id) {
        	logger.info("update " + id);
                Map<String,Object> model = new HashMap<String,Object>();
                Object sale = saleBO.selectById(id);
                model.put("sale", sale);
               return new ModelAndView("UpdateSale",model);

        }
        
        /**
         * handles save update sale request. In the jsp we are using spring's form:form tags
         * Spring expects a model called by default 'command' or any name
         * we set to it.
         * @return the name of the view to show
         * @RequestMapping({"/sale/saveUpdate"})
         */
        @RequestMapping(method=RequestMethod.POST,value={"/sale/saveupdate"})
        public String saveUpdate(@Valid Sale sale, BindingResult bindingResult) {
        		logger.info("update sale " + sale.toString());
        		
        		// In case of validation errors, go back to form
        		if (bindingResult.hasErrors()) {
        			Map<String, Object> model = new HashMap<String, Object>();
        			model.put("sale", sale);
        			return "UpdateSale";
        		}
        		
                saleBO.update(sale);
                return "redirect:/sale";
        }
}
